import { ArbitratorSchema } from '@src/components/schema/arbitrator'
import { ColumnsType } from 'antd/es/table'
import { Space, Divider, Modal, message } from 'antd'
import style from './index.module.less'
import { ExclamationCircleOutlined } from '@ant-design/icons'
import { delRequest } from './ajax'
import { awaitWrap } from '@src/assets/js/tool'

const { confirm } = Modal

// 二次确认退回
export function confrimWithdraw (data: ArbitratorSchema, refreshData: () => void): void {
  confirm({
    icon: <ExclamationCircleOutlined />,
    content: `您确认要删除 ${data.name} 吗？`,
    onOk: () => {
      delHandle(data.id, refreshData).catch(e => console.error(e))
    }
  })
}

async function delHandle (id: string, refreshData: () => void): Promise<void> {
  const hide = message.loading('删除中', 120)
  const [e] = await awaitWrap(delRequest(id))
  hide()
  if (e === null) {
    message.success('删除成功').then(null, null)
    refreshData()
  }
}

export function getColumn (setVisible: (v: boolean) => void, setModifyData: (v: ArbitratorSchema) => void, refreshData: () => void): ColumnsType<ArbitratorSchema> {
  const columns: ColumnsType<ArbitratorSchema> = [
    {
      title: '序号',
      dataIndex: 'orderno',
      key: 'orderno',
      width: 75
    },
    {
      title: '姓名',
      dataIndex: 'name',
      key: 'name'
    },
    {
      title: '职业分类',
      dataIndex: 'occupation',
      key: 'occupation'
    },
    {
      title: '所在区域',
      dataIndex: 'area',
      key: 'area'
    },
    {
      title: '专业',
      dataIndex: 'professional',
      key: 'professional'
    },
    {
      title: '职务',
      dataIndex: 'structure_post',
      key: 'structure_post'
    },
    {
      title: '操作',
      dataIndex: 'id',
      key: 'id',
      render: (v: string, l: ArbitratorSchema) => {
        return (
          <Space split={<Divider type='vertical' />}>
            <a onClick={() => { setModifyData(l); setVisible(true) }}>修改</a>
            <a className={style.del} onClick={() => confrimWithdraw(l, refreshData)}>删除</a>
          </Space>
        )
      }
    }
  ]
  return columns
}
